package inc;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageTool {

	/**
	 * 根据给定的源图文件名和目标图片文件名，以及要缩小的目标尺寸，将图片缩小后输出
	 * @param srcImgFileName      String 源文件名，包括文件路�?
	 * @param targetImgFileName   String 目标文件名，包括文件路径
	 * @param imgWidth            int 目标图片�?
	 * @param imgHeight           int 目标图片�?
	 * @throws Exception
	 */
	
	/*
	 * get an Image object
	 */
	public Image getImage(String sourceFile) {
		File _file = new File(sourceFile);
		Image img = null;
		try {
			img = javax.imageio.ImageIO.read(_file);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("ImageTool.getImage: read source file to Image File error!");
			e.printStackTrace();
		}
		return img;
	}
	
	public void zoomImage(String srcImgFileName, String targetImgFileName,
			int imgWidth, int imgHeight) throws Exception{
		//读入文件
		File _file = new File(srcImgFileName);
		//构�?�Image对象
		BufferedImage src = javax.imageio.ImageIO.read(_file);
		BufferedImage tag = new  BufferedImage(imgWidth,imgHeight,BufferedImage.TYPE_INT_RGB);
		//绘制缩小后的�?
		tag.getGraphics().drawImage(src,0,0,imgWidth,imgHeight,null);
		//输出到文件流
		FileOutputStream out=new FileOutputStream(targetImgFileName);
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		//近JPEG编码
		encoder.encode(tag);
		out.close();
	}
	
	/*
	 * function: reduce source image to target image by keeping ratio
	 * source : source file（complete path �?
	 * target:  target file(complete path)
	 * type : by what? height or width
	 * value : the value of type
	 */
	public void cutImgWithRatio(String source,String target, String type, int typeValue) {
		File fimg = new File(source);//读入文件    read file
		try {
			Image img = javax.imageio.ImageIO.read(fimg);
			ImageTool ci = new ImageTool();
			int newHeight = 10;
			int newWidth = 10;
			int oldHeight = img.getHeight(null);
			int oldWidth = img.getWidth(null);
			if(type.equals("height")) {
				newHeight = typeValue;
				float ratio = (float)newHeight/oldHeight;
				newWidth = (int) (oldWidth*ratio);
			}else if(type.equals("width")) {
				newWidth = typeValue;
				float ratio = (float)newWidth/oldWidth;
				newHeight = (int) (oldHeight*ratio);
			}
			
			try {
				ci.zoomImage(source, target, newWidth, newHeight);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 运行主程�?
	 * @param args
	 */
	public static void main(String[] args) {
		/*
		String imgFileName = "F:/img/1.jpg";
/*
		if (args.length >= 1) {
			imgFileName = args[0];
		} else {
			System.err.println("Usage: cmd imgFileName");
			System.exit (1);
		}
*/		
		/*
		try {
			String targetImgFileName = "targetIMG.jpg";
			CutImage iZoom = new CutImage();
			iZoom.zoomImage("http://localhost:8080/default/attached/20100822151140_430[1].jpg", "http://localhost:8080/default/attached/20100822151140_430[1].jpg", 350, 360);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		*/
		ImageTool ci = new ImageTool();
		
		ci.cutImgWithRatio("E:/RPC/a1.jpg", "E:/RPC/_a1.jpg", "height", 100);
	}

}
